package bjfu.it.sun.chat;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.List;

public class RecorderAdapter extends ArrayAdapter<VoiceMessage> {
    private int mMinItemWidth;
    private int mMaxItemWidth;
    private LayoutInflater mInflater;
    public RecorderAdapter(@NonNull Context context, List<VoiceMessage> datas) {
        super(context, -1 ,datas);
        mInflater = LayoutInflater.from(context);
//获取屏幕参数
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
//设置最小宽度和最大宽度
        mMinItemWidth = (int) (outMetrics.widthPixels * 0.16f);
        mMaxItemWidth = (int) (outMetrics.widthPixels * 0.64f);
    }
    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        ViewHolder holder = null;
        if(convertView == null){
            convertView = mInflater.inflate(R.layout.item_recorder,parent,false);
            holder = new ViewHolder();
            holder.duration = (TextView) convertView.findViewById(R.id.duration);
            holder.distance = (TextView) convertView.findViewById(R.id.distance);
            holder.touxiang=(ImageView) convertView.findViewById(R.id.touxiang);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
//设置时长
        holder.duration.setText(Math.round(getItem(position).duration)+ "s");
        holder.distance.setText("距离我"+getItem(position).distance+ "米");
        holder.touxiang.setImageResource(R.drawable.image1);
        return convertView;
    }
    private class ViewHolder{
        TextView duration;
        TextView distance;
        ImageView touxiang;
    }
}
